package com.tg.app.view;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.tg.app.R;

public class CameraBatteryStatusView extends LinearLayout {
    private ImageView statusView;
    private int mBattery, mBatteryMode = 0;
    private Context mContext;
    public CameraBatteryStatusView(Context context) {
        super(context);
        init(context);
    }

    public CameraBatteryStatusView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CameraBatteryStatusView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);

    }
    private void init(Context context){
        mContext = context;
        statusView = new ImageView(context);
        addView(statusView);
    }


    public void  updateBatteryStatus(int battery, int mode){

        if (battery == -1 && mode == -1) {
            setVisibility(GONE);
            return;
        }
        if (getVisibility() == GONE) {
            setVisibility(VISIBLE);
        }
        if (mode == mBatteryMode && mBattery == battery) return;
        mBatteryMode = mode;
        mBattery = battery;
        if (mode == 0) {
            setBackgroundResource(R.drawable.camera_player_battery_bg);
            if (battery == 100) {
                statusView.setImageResource(R.mipmap.icon_camera_player_battery_100);
            } else if (battery >= 80) {
                statusView.setImageResource(R.mipmap.icon_camera_player_battery_90);
            } else if (battery >= 60) {
                statusView.setImageResource(R.mipmap.icon_camera_player_battery_70);
            } else if (battery >= 40) {
                statusView.setImageResource(R.mipmap.icon_camera_player_battery_50);
            } else if (battery >= 20) {
                statusView.setImageResource(R.mipmap.icon_camera_player_battery_30);
            } else {
                statusView.setImageResource(R.mipmap.icon_camera_player_battery_10);
            }
        } else {
            if (battery == 100) {
                setBackgroundResource(R.drawable.camera_player_battery_bg);
                statusView.setImageResource(R.mipmap.icon_camera_player_battery_100_charging);
            } else {
                setBackgroundResource(R.drawable.camera_player_battery_charging_bg);
                int resID = R.mipmap.battery_charging_0;
                if (battery >= 70) {
                    resID = R.mipmap.battery_charging_70;
                } else if (battery >= 30) {
                    resID = R.mipmap.battery_charging_30;
                }
                Glide.with(mContext).asGif().load(resID).into(statusView);
            }
        }
    }
    
}
